﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace TestClipboard
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern bool AddClipboardFormatListener(IntPtr hwnd);

        [DllImport("user32.dll")]
        public static extern bool RemoveClipboardFormatListener(IntPtr hwnd);

        public Form1()
        {
            AddClipboardFormatListener(this.Handle);

            InitializeComponent();
            thread = new Thread(Loop);
            if (!thread.IsAlive)
                thread.Start();

        }
        int WM_CLIPBOARDUPDATE = 0x031D;
        string[] strings = new[]
              {
                "DataObjectAttributes",
                "DataObjectAttributesRequiringElevation",
                "FileGroupDescriptorW",
                "FileContents",
                "Preferred DropEffect",
                "UIDisplayed",
                "DropDescription",
                "AsyncFlag",
                "TVClipboard",
            };

        private Thread thread;
        void Loop()
        {
            while (true)
                foreach (var str in strings)
                {
                    if (Clipboard.ContainsData(str))
                    {
                        MemoryStream stream = (MemoryStream)Clipboard.GetData(str);
                        stream.GetBuffer();
                        Trace.WriteLine(stream.GetBuffer());

                        Trace.WriteLine($"{str}的流信息：");
                        Thread.Sleep(1000);
                    }
                }
        }
        protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == WM_CLIPBOARDUPDATE)
            {
                IDataObject iData = Clipboard.GetDataObject();
                if (Clipboard.ContainsFileDropList())
                {

                }
                if (Clipboard.ContainsAudio())
                {

                }
                foreach (var str in strings)
                {
                    if (Clipboard.ContainsData(str))
                    {
                        try
                        {
                            MemoryStream stream = (MemoryStream) Clipboard.GetData(str);
                            //stream.GetBuffer();
                            //Trace.WriteLine(stream.GetBuffer());

                            //Trace.WriteLine($"{str}的流信息：");
                            //Thread.Sleep(1000);
                        }
                        catch (Exception)
                        {
                            
                        }
                    }
                }
            }
            else
            {
                base.DefWndProc(ref m);
            }
        }
    }
}
